package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.FiveGModelAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author chen.cheng110
 * @version v1.0
 * @date 2021/12/8 17:24
 */
@Service
@AttrHandler(FiveGModelAttrHandler.class)
public class FiveGModelAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        // 处理框架产品与子产品的型号属性联动
        this.modelAttrLinkHandle(attr, inst, data);
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    private void modelAttrLinkHandle(KiteAttr attr, KiteInst inst, AttrData data) {
        String offerInstId = querySceneInstService.getSceneInst(data.getSceneInstId()).getRootInstId();
        List<KiteProdInst> prodInstList = querySceneInstService.listProdInstsFromGiftOfferInst(data.getCustId(),
            offerInstId);
        prodInstList.forEach(prodInst -> {
            if (KiteStringUtils.equals(inst.getInstId(), prodInst.getInstId()) || Objects
                .isNull(prodInst.getAttr("200002772"))) {
                return;
            }
            // 雁飞5G模组部件 定制5G连接产品（融合）
            if (!KiteStringUtils.equalsAny(prodInst.getSpecId(), "60049211","50040001")) {
                return;
            }
            operateInstAttrService.setAttrValue(prodInst, "200002772", attr.getValue(),
                data.getSceneInstId(), null);
        });
    }
}
